/**
 * 
 */
package com.ermax.aircond.common.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ermax.aircond.common.dao.ReadMachineDataAuditDao;
import com.ermax.aircond.common.dao.ReadMachineDataDao;
import com.ermax.aircond.common.domain.ReadMachineData;
import com.ermax.aircond.common.domain.ReadMachineDataAudit;
import com.ermax.aircond.util.service.CalendarService;

/**
 * @author Eric
 *
 */

@Service("readMachineDataService")
public class ReadMachineDataService {

	private static final Logger logger = Logger.getLogger(ReadMachineDataService.class);
	
	@Resource(name="readMachineDataDao")
	private ReadMachineDataDao readMachineDataDao;
	
	@Resource(name="readMachineDataAuditDao")
	private ReadMachineDataAuditDao readMachineDataAuditDao;
	
	@Resource(name = "calendarService")
	private CalendarService calendarService;

	
	//interval type
	public static final int INTERVAL_TYPE_1HOUR = 1;
	public static final int INTERVAL_TYPE_6HOUR = 2;
	public static final int INTERVAL_TYPE_1DAY = 3;
	
	/* (non-Javadoc)
	 * @see com.ermax.aircond.common.service.ReadMachineDataService#selectByNid(int)
	 */
	
	public ReadMachineData selectByNid(int nid) {
		return readMachineDataDao.getByNid(nid);		
	}

	/* (non-Javadoc)
	 * @see com.ermax.aircond.common.service.ReadMachineDataService#select(int)
	 */	
	public ReadMachineData select(int idx) {		
		return readMachineDataDao.getById(idx);
	}
	
	/*
	public void updateRcmd(int nid) {
		readMachineDataDao.updateRcmd(nid);
	}
	*/
	
	public List<ReadMachineData> getErrorNidData(String errorSignal) {
		return readMachineDataDao.getErrorNidData(errorSignal);
	}
		
	public int insert(ReadMachineData readMachineData) {
		// TODO Auto-generated method stub
		logger.debug("Insert!");
		readMachineDataDao.create(readMachineData);
		return 0;
	} 
	
		
	public int getMaxReadMachineDataIdx() {
		return readMachineDataDao.getMaxReadMachineDataIdx();
	}
	
	public void delete(ReadMachineData readMachineData){
		readMachineDataDao.delete(readMachineData);
	}
	
	public int getIntervalType(Date fromDate, Date toDate){
		int dayDiff = this.calendarService.getDayDiff(fromDate, toDate);
		if(dayDiff > 7){
			return INTERVAL_TYPE_1DAY;
		}else if(dayDiff > 0){
			return INTERVAL_TYPE_6HOUR;
		}else{
			return INTERVAL_TYPE_1HOUR;
		}
		
	}
	
	@Transactional(readOnly=true)
	public List<ReadMachineDataAudit> getReadMachineDataAudits(int nid, Date fromDate, Date toDate){
		fromDate = calendarService.convertStartOftheDate(fromDate);
		toDate = calendarService.convertEndOftheDate(toDate);			
		
		return readMachineDataAuditDao.findMachineDateByNid(nid, fromDate, toDate);
	}
		
	
	@Transactional(readOnly=true)
	public List<Object[]> getReadMachineDataAuditsGroupByDay(int nid, Date fromDate, Date toDate){
		fromDate = calendarService.convertStartOftheDate(fromDate);
		toDate = calendarService.convertEndOftheDate(toDate);			
		
		return readMachineDataAuditDao.findMachineDataByNidGroupByDay(nid, fromDate, toDate);
	}
	
	@Transactional(readOnly=true)
	public List<Object[]> getReadMachineDataAuditsGroupByHour(int nid, Date fromDate, Date toDate){
		fromDate = calendarService.convertStartOftheDate(fromDate);
		toDate = calendarService.convertEndOftheDate(toDate);			
		
		return readMachineDataAuditDao.findMachineDataByNidGroupByHour(nid, fromDate, toDate);
	}
	
	@Transactional(readOnly=true)
	public List<Object[]> getReadMachineDataAuditsGroupBySixHour(int nid, Date fromDate, Date toDate){
		fromDate = calendarService.convertStartOftheDate(fromDate);
		toDate = calendarService.convertEndOftheDate(toDate);			
		
		return readMachineDataAuditDao.findMachineDataByNidGroupBySixHour(nid, fromDate, toDate);
	}
}
